package com.wangyadong.hobby.schedule.jobtask.dsa;

/**
 * @ClassName ArrayList
 * @Description TODO
 * @Authpr Antonio
 * @Data 2018-07-19 13:12
 */
public class ArrayList1<E> implements List {
    private static final int CAPACITY = 16;
    private int size;
    private E[] data;


    public ArrayList1() {
        this(CAPACITY);
    }

    public ArrayList1(int capacity) {
        data = (E[]) new Object[capacity];
    }

    public int size() {
        return size;
    }

    public boolean isEmpty() {
        return size() == 0;
    }

    public E get(int i) {
        checkNum(i, size);
        return data[i];
    }

    /*public E add(int i, E data) throws IndexOutOfBoundsException,IllegalStateException {
        checkNum(i, size + 1);
        return null;
    }*/


    @Override
    public Object set(int i, Object e) throws IndexOutOfBoundsException {
        return null;
    }

    @Override
    public void add(int i, Object e) throws IndexOutOfBoundsException {
        checkNum(i, size + 1);
        if (size == data.length)
            throw new IllegalStateException("===");
        for (int k = size - 1; k >= i; k--) {
            System.out.println("k and vlaue =" + k + "  " + data[k]);
            data[k + 1] = data[k];
        }
        data[i] = (E) e;
        size++;

    }

    @Override
    public Object remove(int i) throws IndexOutOfBoundsException {
        return null;
    }

    private void checkNum(int i, int sizeN) {
        if (i < 0 || i >= sizeN)
            throw new IndexOutOfBoundsException("is full");
    }

    public static void main(String[] args) {
        ArrayList1 arrayList1 = new ArrayList1(5);
        arrayList1.add(0, 1);
        arrayList1.add(1, 1);
        arrayList1.add(2, 1);
        arrayList1.add(3, 1);
        arrayList1.add(4, 1);

//        arrayList1.add(5,1);
//        arrayList1.add(6,1);
//        arrayList1.add(7,1);
//        arrayList1.add(8,1);
//        arrayList1.add(9,1);
//        arrayList1.add(10,1);
    }

}
